﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;
using ShopOnline.Controls;

namespace ShopOnline
{
    public partial class MyWishList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (View_Customers.GetCurrentUser() == null)
            {
                Response.Redirect("Register.aspx");
                return;
            }

            if (!IsPostBack)
            {
                BannerKey = ShopOnline.BLL.BannerKey.Wishlist;
                ((ProductMaster)this.Master).SetPageTitle("My WishList");
                SortDir = SORT_ASC;
                SearchFilter = View_WishList.ColumnNames.CustomerID + "=" + View_Customers.GetCurrentUser().s_ID;
                wishList.Rebind();
            }
        }

        public string SearchFilter
        {
            get { return ViewState["SearchFilter"].ToString(); }
            set { ViewState["SearchFilter"] = value; }
        }

        public string SortExpression
        {
            get { return SortBy + SortDir; }
        }

        public string SortBy
        {
            get { return ddlSortBy.SelectedValue; }
            set { ddlSortBy.SelectedValue = value; ddlSortByDown.SelectedValue = value; }
        }

        public string SortDir
        {
            get { return ViewState["SortDir"].ToString(); }
            set { ViewState["SortDir"] = value; }
        }

        string GetCatID()
        {
            string catparm = Request.QueryString[CAT];
            int lastIndex = catparm.LastIndexOf("_");
            return catparm.Substring(lastIndex + 1);
        }

        protected void wishList_OnCartChanged(object sender, EventArgs e)
        {
            ((ProductMaster)this.Master).SetCartTitle(string.Format("You have <a href='Cart.aspx'>{0} item(s)</a> in your shopping cart.", BasePage.SelectedItems.RowCount));
        }

        protected void btnSortDir_OnClick(object sender, EventArgs e)
        {
            if (SortDir == SORT_ASC)
            {
                SortDir = SORT_DESC;
                imgSortBy.ImageUrl = "~/images/i_desc_arrow.gif";
                imgSortDirDown.ImageUrl = "~/images/i_desc_arrow.gif";
            }
            else
            {
                SortDir = SORT_ASC;
                imgSortBy.ImageUrl = "~/images/i_asc_arrow.gif";
                imgSortDirDown.ImageUrl = "~/images/i_asc_arrow.gif";
            }

            wishList.Rebind();
        }

        int PageSize
        {
            get { return dataPager.PageSize; }
            set
            {
                dataPager.PageSize = value;
                dataPagerDown.PageSize = value;
                ddlItemPerRow.SelectedValue = value.ToString();
                ddlItemPerRowDown.SelectedValue = value.ToString();
            }
        }

        int CurrentPageIndex
        {
            get { return dataPager.CurrentPageIndex; }
            set
            {
                dataPager.CurrentPageIndex = value;
                dataPagerDown.CurrentPageIndex = value;
            }
        }

        int TotalItems
        {
            get { return dataPager.TotalItems; }
            set
            {
                dataPager.TotalItems = value;
                dataPagerDown.TotalItems = value;
                lblTotalItems.Text = string.Format("{0} Item(s)", value);
                lblTotalItemsDown.Text = string.Format("{0} Item(s)", value);
            }
        }

        protected void ddlItemPerRow_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            PageSize = int.Parse(ddl.SelectedValue);
            wishList.Rebind();
        }

        protected void dataPager_OnPageIndexChanged(object sender, PageIndexChangedEventArgs e)
        {
            DataPagerCtrl pager = (DataPagerCtrl)sender;
            CurrentPageIndex = pager.CurrentPageIndex;
            wishList.Rebind();
        }

        protected void wishList_OnNeedDataSource(object sender, EventArgs e)
        {
            View_WishList prods = new View_WishList();
            int count = prods.Search(SearchFilter, SortExpression, CurrentPageIndex * PageSize, PageSize);
            wishList.DataSource = prods.DefaultView;
            TotalItems = count;
            lblNoRecord.Visible = count <= 0;
        }

        protected void ddlSortBy_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            SortBy = ddl.SelectedValue;
            wishList.Rebind();
        }
    }
}